package com.csdj.service;

import com.csdj.dao.UserMapper;
import com.csdj.query.UserQuery;
import com.csdj.utils.AssertUtil;
import com.csdj.vo.User;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserService {

    @Autowired(required = false)
    private UserMapper userMapper;

      //区分每个参数对应的数据
    @Cacheable(value = "users",key = "#uname")
    public User queryUserByName(String uname){
        return userMapper.queryUserByName(uname);
    }


    /**
     * 根据id查询用户信息
     * @param id
     * @return
     */
    @Cacheable(value = "users",key = "#id")
    public User queryUserById(Integer id){

        return userMapper.queryById(id);
    }

    /**
     * 添加 用户信息
     * @param user
     */
   // @CachePut(value = "users",key = "#user.id+'-'+#user.userName")
    @Caching(put = {
            @CachePut(value = "users",key = "#user.id"),
            @CachePut(value = "users",key = "#user.userName"),
            @CachePut(value = "users",key = "#user.userPwd")
    })
    public void save(User user){
        AssertUtil.isTrue(StringUtils.isBlank(user.getUserName()),
                "用户名不能为空!");
        AssertUtil.isTrue(StringUtils.isBlank(user.getUserPwd()),"密码不能为空!");
        User temp = userMapper.queryUserByName(user.getUserName());
        AssertUtil.isTrue(temp!=null,"该用户已存在!");
        AssertUtil.isTrue(userMapper.save(user)<1,"用户添加失败!");

    }

    /*@Caching(put = {
            @CachePut(value = "users",key = "#user.id"),
            @CachePut(value = "users",key = "#user.userName"),
            @CachePut(value = "users",key = "#user.userPwd")
    })*/
    @CacheEvict(value = "users",key = "#user.id")
    public void update(User user){
        AssertUtil.isTrue(StringUtils.isBlank(user.getUserName()),
                "用户名不能为空!");
        AssertUtil.isTrue(StringUtils.isBlank(user.getUserPwd()),"密码不能为空!");
        User temp = userMapper.queryById(user.getId());
        AssertUtil.isTrue(temp==null,"该记录不存在");
        AssertUtil.isTrue(userMapper.update(user)<1,"修改信息失败！");

    }

    /**
     * 根据id 删除用户信息
     * @param id
     */
    @CacheEvict(value = "users",allEntries = true) //删除完清理缓存
    public void delete(Integer id){
        User temp = userMapper.queryById(id);
        AssertUtil.isTrue(temp==null,"该记录不存在!");
        AssertUtil.isTrue(userMapper.delete(id)<1,"删除失败!");
    }


    @Cacheable(value = "users",key = "#userQuery.userName+'-'+#userQuery.pageNum+'-'+#userQuery.pageSize")
    public PageInfo<User> queryUserByParams(UserQuery userQuery){
        //1.设置 分页的所需参数
        PageHelper.startPage(userQuery.getPageNum(),userQuery.getPageSize());
        //2.实例化 pageInfo 进行分页 传入所需要的数据
        List<User> userList = userMapper.selectByParams(userQuery);
        PageInfo<User> pageInfo=new PageInfo<User>(userList);
        return pageInfo;
    }




}
